<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '130px' : '110px'"
    >
      <el-form-item label="奖金类型" prop="bonusTypeId">
        <el-select
          style="width: 300px;"
          v-model="dataForm.bonusTypeId"
          clearable
          placeholder="请选择奖金类型"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.typeName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="奖金名称" prop="title">
        <el-input style="width: 300px;" v-model="dataForm.title" placeholder="奖金名称"></el-input>
      </el-form-item>
      <el-form-item label="奖金金额" prop="money">
        <el-input-number
          style="width: 300px;"
          :precision="2"
          :min="0"
          :controls="false"
          v-model="dataForm.money"
          placeholder="奖金金额"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="开始时间" prop="startDate">
        <el-date-picker
          style="width: 300px;"
          v-model="dataForm.startDate"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="开始时间"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker
          style="width: 300px;"
          v-model="dataForm.endDate"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最低绩点要求" prop="minGpa">
        <el-input-number
          style="width: 300px;"
          :precision="2"
          :min="0"
          :controls="false"
          v-model="dataForm.minGpa"
          placeholder="绩点要求"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="最低综测分要求" prop="minTestingScore">
        <el-input-number
          style="width: 300px;"
          :precision="2"
          :min="0"
          :controls="false"
          v-model="dataForm.minTestingScore"
          placeholder="综测分要求"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="最低困难等级要求" prop="minDifficultyLevel">
          <el-select
            style="width: 300px;"
            v-model="dataForm.minDifficultyLevel"
            clearable
            placeholder="请选择最低困难等级"
          >
            <el-option
              v-for="item in difficultyLevelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="特殊条件" prop="conditionTest">
        <el-input
          style="width: 400px;"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          v-model="dataForm.conditionTest"
          placeholder="特殊条件"
        ></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() < new Date().getTime() - 86400000;
        },
      },
      visible: false,
      dataForm: {
        id: "",
        bonusTypeId: "",
        title: "",
        conditionTest: "",
        money: "",
        startDate: "",
        endDate: "",
        minGpa: "",
        minTestingScore: "",
        minDifficultyLevel: ""
      },
      typeList: [],
      difficultyLevelList: [
        {
          label: '暂无',
          value: 0
        },
        {
          label: '一般困难',
          value: 1
        },
        {
          label: '中等困难',
          value: 2
        },
        {
          label: '非常困难',
          value: 3
        }
      ]
    };
  },
  created() {
    this.listCategories();
  },
  computed: {
    dataRule() {
      return {
        bonusTypeId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        title: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        condition: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        money: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        creator: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        createDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        updater: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        updateDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    listCategories() {
      this.$http.get("/bonus/bonustype/list").then(({ data }) => {
        this.typeList = data.data;
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/bonus/bonuslist/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/bonus/bonuslist/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
